math.randomseed(os.time())
 


items = {}
itemlen=1
framecount=0;

player = { x = 2, y = 250, speed = 2., score  = 0, img = Image.createEmpty(16,16) }
player.img:clear(Color.new(255,128,64))
green = Color.new(0, 255, 0)
red = Color.new(255,0 , 0)



function playerMovement()
pad = Controls.read()

if pad:left() and player.x > 0 then
	player.speed=player.speed -0.4
end

if pad:right() and player.x < ( 480 - player.img:width() ) then
	player.speed=player.speed +0.4
end
player.x = player.x + player.speed
player.speed = player.speed *0.95
if player.x<0 then player.x= 0 player.speed=0 end
if player.x>480 -player.img:width() then player.x= 480 -player.img:width() player.speed=0 end

end


function dropItems()
i=0
while i  < itemlen do
	if (items[i]==nil or items[i].type=="erased") then
	if math.random(10) == 1 then
		items[i]={type="bad",x=math.random(10,470),y=-50,speed= math.random()*2.0+0.1,img=Image.createEmpty(math.random(10,50), math.random(10,50))}
		items[i].img:clear(red)
		else
		items[i]={type="good",x=math.random(10,470),y=-50,speed= math.random()*2.0+0.1,img=Image.createEmpty(math.random(10,50), math.random(10,50))}
		items[i].img:clear(green)
	end
	end    
	i = i + 1
end

--itemlen=itemlen+1;

end

function collisions()
i=0
while i  < itemlen do
	items[i].y=items[i].y+items[i].speed;
	if (player.x < items[i].x+items[i].img:width() ) and ( player.x+player.img:width()>items[i].x) and 
       (player.y < items[i].y+items[i].img:height() ) and ( player.y+player.img:height()>items[i].y) then
		   if (items[i].type=="good") then player.score=math.floor(player.score+(items[i].img:height()*items[i].img:width())*items[i].speed)
		   elseif (items[i].type=="bad") then player.score=math.floor(player.score-(items[i].img:height()*items[i].img:width())*items[i].speed)
		   end
	   items[i].type="erased"
	end   
	if (items[i].y-items[i].img:height()>272) then
		items[i].type="erased" end
	i = i + 1
end
end



while true do
	framecount=framecount+1
	playerMovement()
	
	dropItems()
	collisions()


	screen:clear()
	screen:print(0, 20, "Score", green)
	i=0
	while i  < itemlen do
	 if(items[i].type=="good") then
	 screen:blit(items[i].x,items[i].y,items[i].img) end
	 i = i + 1
	end

	i=0
	while i  < itemlen do
	 if(items[i].type=="bad") then
	 screen:blit(items[i].x,items[i].y,items[i].img) end
	 i = i + 1
	end
	screen:blit(player.x,player.y,player.img)

	screen:print(50, 20,player.score, green)
	screen.waitVblankStart()
	screen:flip()
	if(framecount== 60*60) then 
		while true do
		screen:clear()
		screen:print(200, 120, "Score", green)
		screen:print(250, 120,player.score, green)
		screen.waitVblankStart()
		screen:flip()
		end
	end
	if framecount % 60==0 then itemlen=itemlen+1 end
end

